<template>
  <div class="aside-container">
    <el-menu
      :default-active="$route.path"
      :collapse="props.isCollapse"
      :collapse-transition="false"
      router
      class="aside-menu"
      background-color="#1d1e1f"
      text-color="#fff"
      active-text-color="#ffd04b"
    >
      <div class="logo-container" :class="{ 'collapsed': props.isCollapse }">
        <img class="logo-img" src="@/assets/logo_transparent.png" alt="logo" />
      </div>

      <div v-for="item in menus" :key="item.id">
        <!-- 没有子菜单的情况 -->
        <el-menu-item v-if="item.path" :index="item.path">
          <el-icon><component :is="getIcon(item.icon)" /></el-icon>
          <template #title>
            <span>{{ item.menuName }}</span>
          </template>
        </el-menu-item>

        <!-- 有子菜单的情况 -->
        <el-sub-menu v-else :index="item.id + ''">
          <template #title>
            <el-icon><component :is="getIcon(item.icon)" /></el-icon>
            <span>{{ item.menuName }}</span>
          </template>
          <el-menu-item
            v-for="subItem in item.children"
            :key="subItem.id"
            :index="subItem.path"
          >
            <template #title>
              <span>{{ subItem.menuName }}</span>
            </template>
          </el-menu-item>
        </el-sub-menu>
      </div>
    </el-menu>
  </div>
</template>

<script setup>
import { ref, defineProps } from 'vue';
import { House, User, Tools, Document } from '@element-plus/icons-vue';

// 定义props
const props = defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
});

// 从 localStorage 获取菜单数据
const menus = ref(JSON.parse(localStorage.getItem("menus")) || []);

// 图标获取函数
const getIcon = (iconName) => {
  const iconMap = {
    'el-icon-s-home': House,
    'el-icon-user': User,
    'el-icon-s-tools': Tools,
    'el-icon-document': Document
  };
  return iconMap[iconName] || Document;
};
</script>

<style scoped>
.aside-container {
  height: 100%;
  background-color: #1d1e1f;
}

.aside-menu {
  border-right: none;
  height: 100%;
}

.aside-menu:not(.el-menu--collapse) {
  width: 200px;
}

.logo-container {
  height: 200px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #1d1e1f;
  overflow: hidden;
}

.logo-container.collapsed {
  height: 60px;
}

.logo-container.collapsed .logo-img {
  width: 32px;
  height: 32px;
  margin: 0;
}

.logo-img {
  width: 200px;
  height: 200px;
  transition: all 0.3s;
}

:deep(.el-menu) {
  border-right: none !important;
}

:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  color: #fff !important;
}

:deep(.el-menu-item.is-active) {
  background-color: #4a5463 !important;
  color: #ffd04b !important;
  font-weight: bold;
}

:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  background-color: #4a5463 !important;
  color: #ffd04b !important;
}

:deep(.el-sub-menu.is-active .el-sub-menu__title) {
  color: #ffd04b !important;
}

:deep(.el-menu-item) .el-icon,
:deep(.el-sub-menu__title) .el-icon {
  width: 16px;
  margin-right: 16px;
  color: inherit;
}

/* 子菜单样式 */
:deep(.el-menu--inline) {
  background-color: #2d2e30 !important;
}

:deep(.el-menu--inline .el-menu-item) {
  background-color: #2d2e30 !important;
  padding-left: 48px !important;
}

:deep(.el-menu--inline .el-menu-item:hover) {
  background-color: #4a5463 !important;
}

:deep(.el-menu--inline .el-menu-item.is-active) {
  background-color: #4a5463 !important;
  color: #ffd04b !important;
}

/* 自定义滚动条样式 */
.aside-menu::-webkit-scrollbar {
  width: 6px;
}

.aside-menu::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.aside-menu::-webkit-scrollbar-track {
  background: #1d1e1f;
}

:deep(.el-sub-menu) {
  background-color: #1d1e1f !important;
}

:deep(.el-menu--collapse) {
  width: 64px;
}

/* 确保文字在各种状态下都清晰可见 */
:deep(.el-menu-item),
:deep(.el-sub-menu__title),
:deep(.el-menu-item span),
:deep(.el-sub-menu__title span) {
  color: #fff !important;
}

:deep(.el-menu-item.is-active),
:deep(.el-menu-item.is-active span),
:deep(.el-sub-menu.is-active .el-sub-menu__title),
:deep(.el-sub-menu.is-active .el-sub-menu__title span) {
  color: #ffd04b !important;
  font-weight: bold;
}

/* 确保图标颜色跟随文字颜色 */
:deep(.el-menu-item .el-icon),
:deep(.el-sub-menu__title .el-icon) {
  color: inherit !important;
}
</style>
